home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / toolkit.jar / content / global / nsDragAndDrop.js < prev    next >
Encoding:
JavaScript  |  2001-05-26  |  7.3 KB  |  207 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  *   Ben Goodger <ben@netscape.com> (Original Author)
  22.  */
  23.  
  24. ////////////////////////////////////////////////////////////////////////////
  25. // XXX - WARNING - DRAG AND DROP API CHANGE ALERT - XXX
  26. // This file has been extensively modified in a checkin planned for Mozilla
  27. // 0.8, and the API has been modified. DO NOT MODIFY THIS FILE without 
  28. // approval from ben@netscape.com, otherwise your changes will be lost. 
  29.  
  30. /**
  31.  * XXX - until load is supported in chrome, you also need to include 
  32.  *       these files:
  33.  *       chrome://global/content/nsJSSupportsUtils.js
  34.  *       chrome://global/content/nsTransferable.js
  35.  **/
  36.  
  37.  
  38.  
  39. /**
  40.  * nsDragAndDrop - a convenience wrapper for nsTransferable, nsITransferable
  41.  *                 and nsIDragService/nsIDragSession. 
  42.  *
  43.  * USAGE INFORMATION: see 'README-nsDragAndDrop.html' in the same source directory
  44.  *                    as this file (typically xpfe/global/resources/content)
  45.  */
  46.  
  47. var nsDragAndDrop = {
  48.   
  49.   _mDS: null,
  50.   get mDragService()
  51.     {
  52.       if (!this._mDS) 
  53.         {
  54.           const kDSContractID = "@mozilla.org/widget/dragservice;1";
  55.           const kDSIID = Components.interfaces.nsIDragService;
  56.           this._mDS = Components.classes[kDSContractID].getService(kDSIID);
  57.         }
  58.       return this._mDS;
  59.     },
  60.  
  61.   /**
  62.    * void startDrag (DOMEvent aEvent, Object aDragDropObserver) ;
  63.    *
  64.    * called when a drag on an element is started.
  65.    *
  66.    * @param DOMEvent aEvent
  67.    *        the DOM event fired by the drag init
  68.    * @param Object aDragDropObserver
  69.    *        javascript object of format described above that specifies
  70.    *        the way in which the element responds to drag events.
  71.    **/  
  72.   startDrag: function (aEvent, aDragDropObserver)
  73.     {
  74.       if (!("onDragStart" in aDragDropObserver))
  75.         return;
  76.  
  77.       const kDSIID = Components.interfaces.nsIDragService;
  78.       var dragAction = { action: kDSIID.DRAGDROP_ACTION_COPY + kDSIID.DRAGDROP_ACTION_MOVE + kDSIID.DRAGDROP_ACTION_LINK };
  79.  
  80.       var transferData = { data: null };
  81.       try 
  82.         {
  83.           aDragDropObserver.onDragStart(aEvent, transferData, dragAction);
  84.         }
  85.       catch (e) 
  86.         {
  87.           return;  // not a draggable item, bail!
  88.         }
  89.  
  90.       if (!transferData.data) return;
  91.       transferData = transferData.data;
  92.       
  93.       var transArray = nsJSSupportsUtils.createSupportsArray();
  94.       var count = 0;
  95.       do 
  96.         {
  97.           var trans = nsTransferable.set(transferData._XferID == "TransferData" 
  98.                                          ? transferData 
  99.                                          : transferData.dataList[count++]);
  100.           transArray.AppendElement(trans.QueryInterface(Components.interfaces.nsISupports));
  101.         }
  102.       while (transferData._XferID == "TransferDataSet" && 
  103.              count < transferData.dataList.length);
  104.       
  105.       this.mDragService.invokeDragSession(aEvent.target, transArray, null, dragAction.action);
  106.       aEvent.preventBubble();
  107.     },
  108.  
  109.   /** 
  110.    * void dragOver (DOMEvent aEvent, Object aDragDropObserver) ;
  111.    *
  112.    * called when a drag passes over this element
  113.    *
  114.    * @param DOMEvent aEvent
  115.    *        the DOM event fired by the drag init
  116.    * @param Object aDragDropObserver
  117.    *        javascript object of format described above that specifies
  118.    *        the way in which the element responds to drag events.
  119.    **/
  120.   dragOver: function (aEvent, aDragDropObserver)
  121.     { 
  122.       if (!("onDragOver" in aDragDropObserver)) 
  123.         return;
  124.       if (!this.mDragSession) 
  125.         this.mDragSession = this.mDragService.getCurrentSession();
  126.       if (this.mDragSession)
  127.         {
  128.           var flavourSet = aDragDropObserver.getSupportedFlavours();
  129.           for (var flavour in flavourSet.flavourTable)
  130.             {
  131.               if (this.mDragSession.isDataFlavorSupported(flavour))
  132.                 {
  133.                   this.mDragSession.canDrop = (this.mDragSession.sourceNode != aEvent.target);
  134.                   aDragDropObserver.onDragOver(aEvent, 
  135.                                                flavourSet.flavourTable[flavour], 
  136.                                                this.mDragSession);
  137.                   aEvent.preventBubble();
  138.                   break;
  139.                 }
  140.             }
  141.         }
  142.     },
  143.  
  144.   mDragSession: null,
  145.  
  146.   /** 
  147.    * void drop (DOMEvent aEvent, Object aDragDropObserver) ;
  148.    *
  149.    * called when the user drops on the element
  150.    *
  151.    * @param DOMEvent aEvent
  152.    *        the DOM event fired by the drag init
  153.    * @param Object aDragDropObserver
  154.    *        javascript object of format described above that specifies
  155.    *        the way in which the element responds to drag events.
  156.    **/
  157.   drop: function (aEvent, aDragDropObserver)
  158.     {
  159.       if (!("onDrop" in aDragDropObserver))
  160.         return;
  161.         
  162.       if (!this.mDragSession) 
  163.         this.mDragSession = this.mDragService.getCurrentSession();
  164.       if (this.mDragSession)
  165.         {
  166.           var flavourSet = aDragDropObserver.getSupportedFlavours();
  167.           var transferData = nsTransferable.get(flavourSet, this.getDragData, true);
  168.           aEvent.preventBubble();
  169.           // hand over to the client to respond to dropped data
  170.           var multiple = "canHandleMultipleItems" in aDragDropObserver && aDragDropObserver.canHandleMultipleItems;
  171.           var dropData = multiple ? transferData : transferData.first.first;
  172.           aDragDropObserver.onDrop(aEvent, dropData, this.mDragSession);
  173.         }
  174.     },
  175.  
  176.   dragExit: function (aEvent, aDragDropObserver)
  177.     {
  178.       if ("onDragExit" in aDragDropObserver)
  179.         aDragDropObserver.onDragExit(aEvent, this.mDragSession);
  180.     },  
  181.     
  182.   /** 
  183.    * nsISupportsArray getDragData (Object aFlavourList)
  184.    *
  185.    * Creates a nsISupportsArray of all droppable items for the given
  186.    * set of supported flavours.
  187.    * 
  188.    * @param FlavourSet aFlavourSet
  189.    *        formatted flavour list.
  190.    **/  
  191.   getDragData: function (aFlavourSet)
  192.     {
  193.       var supportsArray = nsJSSupportsUtils.createSupportsArray();
  194.       for (var i = 0; i < nsDragAndDrop.mDragSession.numDropItems; ++i)
  195.         {
  196.           var trans = nsTransferable.createTransferable();
  197.           for (var j = 0; j < aFlavourSet.flavours.length; ++j)
  198.             trans.addDataFlavor(aFlavourSet.flavours[j].contentType);
  199.           nsDragAndDrop.mDragSession.getData(trans, i);
  200.           supportsArray.AppendElement(trans);
  201.         }
  202.       return supportsArray;
  203.     }
  204.  
  205. };
  206.  
  207.